package graphs;

// Arraylist, used for storing data from the database
import java.util.ArrayList;

// The historymaker is needed for retrieving the data from the database
import mechanics.HistoryMaker;
// Needed for reading measurements
import mechanics.Meting;

// Needed for defining time indexes
import org.jfree.data.time.Second;

/**
 * HistoryGraph creates graphics based upon data from the SQL database
 * 
 * @version 31-03-2009
 */
public class HistoryGraph extends Graph {

	// Holds the measurements for the graph
	private ArrayList<Meting> metingen;
	
	// The day on which the data was recorded
	private String day;
	// The month on which the data was recorded
	private String month;
	// The year on which the data was recorded
	private String year;
	
	/**
	 * Constructor sets the date parameters and calls the super constructor
	 * 
	 * @param day The day of which we want data
	 * @param month The month of which we want data
	 * @param year The year of which we want data
	 */
	public HistoryGraph(String day, String month, String year) {
		// Method call to the super constructor
		super(day + "/" + month + "/" + year, 30);
		
		// Set the day
		this.day = day;
		// Set the month
		this.month = month;
		// Set the year
		this.year = year;
		
		// Retrieve the requested data from the database
		getData(day + "/" + month + "/" + year + "");
		// Fill the graph with the aquired data
		fillGraph();
	}

	/**
	 * Adds a measurement to the graph
	 * 
	 * @param meting The measurement to add
	 */
	public void addMeting(Meting meting) {
		// Get the hour from the timestamp
		String str_hour = meting.getTime().substring(0, 2);
		// Get the minute from the timestamp
		String str_minute = meting.getTime().substring(3, 5);
		// Get the second from the timestamp
		String str_second = meting.getTime().substring(6, 8);
		
		// Create a time registration mark as a second for use in the graph
		Second second = new Second(Integer.valueOf(str_second), Integer.valueOf(str_minute), Integer.valueOf(str_hour), Integer.valueOf(this.day), Integer.valueOf(this.month), Integer.valueOf(this.year));
		
		// Add the measurement to the graph
		super.addMeting(second, meting);
	}

	/**
	 * Get data from the database
	 * 
	 * @param day The day to get data for
	 */
	private void getData(String day) {
		// Create a historymaker, to retrieve data from the database
		HistoryMaker hs = new HistoryMaker();
		// Get the actual data from the specified day
		this.metingen = hs.retrieveMessures(day);
	}

	/**
	 * Fill the graph with the measurements retrieved from the database
	 */
	private void fillGraph() {
		// For every measurement
		for (int i = 0; i < this.metingen.size(); i++) {
			// Add it to the graph
			this.addMeting(metingen.get(i));
		}
	}
}